'Brendan's Functions
' $HeadURL: http://gabbscript.googlecode.com/svn/trunk/Gabbscript/BrendanFunctions.vb $
' $Author: iwannalightsaber@hotmail.com $
' $Revision: 104 $
' $Date: 2007-11-16 19:47:11 +0000 (Fri, 16 Nov 2007) $

Module BrendanFunctions
    Sub RegisterFunctions(ByRef funcList As FunctionList)
        funcList.RegisterFunction("write", AddressOf consolewrite)
        funcList.RegisterFunction("read", AddressOf consoleread)
        funcList.RegisterFunction("add", AddressOf consoleadd)
        funcList.RegisterFunction("rnd", AddressOf consolernd)
        funcList.RegisterFunction("sub", AddressOf consolesubtract)
        funcList.RegisterFunction("goto", AddressOf consolegoto)
        funcList.RegisterFunction("mul", AddressOf consolemultiply)
        funcList.RegisterFunction("div", AddressOf consoledivide)
        funcList.RegisterFunction("floor", AddressOf consolefloor)
        funcList.RegisterFunction("ceil", AddressOf consoleceil)
        funcList.RegisterFunction("exp", AddressOf consoleexp)
        funcList.RegisterFunction("root", AddressOf consoleroot)
        funcList.RegisterFunction("val", AddressOf consoleval)
        funcList.RegisterFunction("now", AddressOf consolenow)
        funcList.RegisterFunction("hax", AddressOf consolehax)
        funcList.RegisterFunction("beep", AddressOf consolebeep)
        funcList.RegisterFunction("cls", AddressOf consoleclear)
    End Sub
    Function consolewrite(ByVal args As ArgList, ByRef script As Script) As Boolean
        'do not run function if less than 1 argument
        If args.Count < 1 Then Return False

        'for loop amount of arguments written
        For counter As Integer = 0 To args.Count() - 1

            'write the data from argument in the counter to the screen
            Console.Write(args.Item(counter).Data)

        Next

        'go to next line
        Console.WriteLine()

        'return function as executed
        Return True
    End Function
    Function consoleread(ByVal args As ArgList, ByRef script As Script) As Boolean

        'do no run function if less than 1 argument
        If args.Count < 1 Then Return False

        'reads the line that is typed in argument 0
        args.Item(0).Data = Console.ReadLine()

        'return function as executed
        Return True
    End Function
    Function consoleadd(ByVal args As ArgList, ByRef script As Script) As Boolean

        'do not run function if less than 3 arguments
        If args.Count < 3 Then Return False

        'argument 2 is arguments 1 and 2 added together
        args.Item(2).Data = (args.Item(0) + args.Item(1)).Data

        'return function as executed
        Return True
    End Function
    Function consolernd(ByVal args As ArgList, ByRef script As Script) As Boolean

        'do not run function if less than 3 arguments
        If args.Count < 3 Then Return False

        'randomize and set arguments 1 and 2 to min/max variables
        Randomize()

        Dim max As Integer = args.Item(1).Data

        Dim min As Integer = args.Item(0).Data

        'argument 2 is a random number generated using the min/max
        args.Item(2).Data = Int(((max - min + 1) * Rnd() + min))

        'return function as executed
        Return True
    End Function
    Function consolesubtract(ByVal args As ArgList, ByRef script As Script) As Boolean

        'if less than 3 arguments do not run function
        If args.Count < 3 Then Return False

        'argument 2 is argument 0 minus argument 1
        args.Item(2).Data = args.Item(0) - args.Item(1)

        'return function as executed
        Return True
    End Function
    Function consolegoto(ByVal args As ArgList, ByRef script As Script) As Boolean

        'if less than 1 function do not run
        If args.Count < 1 Then Return False

        'set instruction pointer to line in argument 0
        script.SetInstructionPointer(args.Item(0).Data)

        'return function as executed
        Return True
    End Function
    Function consolemultiply(ByVal args As ArgList, ByRef script As Script) As Boolean

        'if less than 3 arguments do not run
        If args.Count < 3 Then Return False

        'argument 2 equals argument 0 times argument 1
        args.Item(2).Data = args.Item(0).Data * args.Item(1).Data

        'return function as executed
        Return True
    End Function
    Function consoledivide(ByVal args As ArgList, ByRef script As Script) As Boolean

        'if less than 3 arguments do not run
        If args.Count < 3 Then Return False

        'argument 2 equals argument 0 divided by argument 1
        args.Item(2).Data = args.Item(0).Data / args.Item(1).Data

        'return function as executed
        Return True
    End Function
    Function consolefloor(ByVal args As ArgList, ByRef script As Script) As Boolean

        'if less than 1 argument do not run
        If args.Count < 1 Then Return False

        'get rid of decimal on number
        Math.Floor(args.Item(0).Data)

        'return function as executed
        Return True
    End Function
    Function consoleceil(ByVal args As ArgList, ByRef script As Script) As Boolean

        'if less than 1 argument do not run
        If args.Count < 1 Then Return True

        'round number up
        Math.Ceiling(args.Item(0).Data)

        'return function as executed
        Return True
    End Function
    Function consoleexp(ByVal args As ArgList, ByRef script As Script) As Boolean

        'if less than 3 arguments do not run
        If args.Count < 3 Then Return False

        'argument 2 equals argument 0 to the power of argument 1
        args.Item(2).Data = args.Item(0).Data ^ args.Item(1).Data

        'return function as executed
        Return True
    End Function
    Function consoleroot(ByVal args As ArgList, ByRef script As Script) As Boolean

        'if less than 3 arguments do not run
        If args.Count < 3 Then Return False

        'root argument 0 from argument 1
        args.Item(2).Data = args.Item(0) ^ (1 / args.Item(1).Data)

        'return as executed
        Return True
    End Function
    Function consoleval(ByVal args As ArgList, ByRef script As Script) As Boolean

        'if less than 2 arguments then return false
        If args.Count < 2 Then Return False

        'the variable in argument 1 stores the value of argument 0
        args.Item(1).Data = Val(args.Item(0))

        'return as executed
        Return True
    End Function
    Function consolenow(ByVal args As ArgList, ByRef script As Script) As Boolean

        'if less than 1 argument return false
        If args.Count < 1 Then Return False

        'create time/date variables
        Dim currenttime As Date

        'assign time of day to date variable and date to date variable
        currenttime = TimeOfDay + Date.Today

        'display the time of day on the screen
        args.Item(0).Data = currenttime.ToString()

        'return function as executed
        Return True
    End Function
    Function consolehax(ByVal args As ArgList, ByRef script As Script) As Boolean

        'define random number variables and instruction pointer variable
        Dim num As Integer
        Dim randnum As Integer
        Dim min As Integer = 1
        Dim max As Integer = 2
        Randomize()

        'num is current line of script
        num = script.GetInstructionPointer()

        'create random number 1 or 2, and either add two or subtract two to the instruction pointer
        randnum = Int((max - min + 1) * Rnd() + min)
        If randnum = 1 Then num = num + 2
        If randnum = 2 Then num = num - 2
        script.SetInstructionPointer(num)

        'set the first 3 arguments to string "gibberish"
        args.Item(0).Data = "gibberish"
        args.Item(1).Data = "gibberish"
        args.Item(2).Data = "gibberish"
    End Function
    Function consolebeep(ByVal args As ArgList, ByRef script As Script) As Boolean

        'if less than 1 argument return false
        If args.Count < 1 Then Return False

        'argument 0 as integer variable
        Dim num As Integer = args.Item(0).Data

        'for every number between 1 and the inputed number, beep once
        For counter As Integer = 1 To num
            Console.Beep()
        Next

        'return function as executed
        Return True
    End Function
    Function consoleclear(ByVal args As ArgList, ByRef script As Script) As Boolean

        'clear the screen
        Console.Clear()

    End Function
End Module

